package com.jiaertangshi.jiaer.service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiaertangshi.jiaer.dao.FoodMapper;
import com.jiaertangshi.jiaer.dao.FoodTypeMapper;
import com.jiaertangshi.jiaer.dao.ShopMapper;
import com.jiaertangshi.jiaer.dto.Food;
import com.jiaertangshi.jiaer.dto.FoodType;
import com.jiaertangshi.jiaer.dto.Shop;
import com.jiaertangshi.jiaer.dto.vo.GetNearbyShopByPage;
import com.jiaertangshi.jiaer.dto.vo.ShopFood;

@Service
public class ShopService {

	@Autowired
	private  ShopMapper  shopMapper;
	
	@Autowired
	FoodMapper  foodMapper;
	
	@Autowired
	FoodTypeMapper  foodTypeMapper;
	
	public   Shop   getAllShop(){
		return 	shopMapper.selectByPrimaryKey(1);
	}


	public List<GetNearbyShopByPage> getNearbyShopByPage(Integer page, Integer size, double jingdu, double weidu, String xianshiqu,int typeid) {
		
		int start = (page - 1) * size;
		List<GetNearbyShopByPage>    gbShops =  shopMapper.getNearbyShopByPage(start,size,jingdu,weidu,xianshiqu,typeid);
		for(int i=0;i<gbShops.size();i++){
		   BigDecimal   shortJuli =   	gbShops.get(i).getJuli();
		   String[]  sh=shortJuli.toString().split("\\.");
		   String  res= sh[0]+"."+sh[1].charAt(0);
		   BigDecimal   shortJuliRes =  new   BigDecimal(res);
		   gbShops.get(i).setJuli(shortJuliRes);
		}
       return gbShops;
	}


	public List<ShopFood> getAllFoodByShop(String shopId) {
		List<ShopFood>    sfRes=new  ArrayList<>(); 
		List<Food>   fds =   foodMapper.getAllFoodByShop(shopId);
		List<Integer>  tys=new  ArrayList<Integer>();
		for(int i=0;i<fds.size();i++){
			tys.add(fds.get(i).getTypeid());
			fds.get(i).setOrderCount(0);
		}
	   HashSet h = new HashSet(tys);  //去除重复的食品类型 
	   tys.clear();   
	   tys.addAll(h);   
		
	   for(int y=0;y<tys.size();y++){
		   ShopFood   food=new  ShopFood();
		   FoodType 	   foodty   = foodTypeMapper.selectByPrimaryKey(tys.get(y));
		   food.setTypeName(foodty.getTypeName());
		   food.setId(foodty.getId());
		   List<Food>   fdsIn = new  ArrayList<Food>(); 
		   for(int   p=0;p<fds.size();p++){
			   if(fds.get(p).getTypeid()==tys.get(y)){
				   fdsIn.add(fds.get(p));
			   }
		   }
		   food.setFs(fdsIn);
		   sfRes.add(food);
	   }
		
		return sfRes;
	}


	public List<GetNearbyShopByPage> serachNearbyShop(Integer page, Integer size, double jingdu, double weidu,String serach) {
		int start = (page - 1) * size;
		List<GetNearbyShopByPage>    gbShops =  shopMapper.serachNearbyShop(start,size,jingdu,weidu,"%" + serach + "%");
		for(int i=0;i<gbShops.size();i++){
		   BigDecimal   shortJuli =   	gbShops.get(i).getJuli();
		   String[]  sh=shortJuli.toString().split("\\.");
		   String  res= sh[0]+"."+sh[1].charAt(0);
		   BigDecimal   shortJuliRes =  new   BigDecimal(res);
		   gbShops.get(i).setJuli(shortJuliRes);
		}
       return gbShops;
	}
	
	
}
